
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php命名空间 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="12_php日期和时间.html" />
    
    
    <link rel="prev" href="10_php面向对象.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01_php介绍.html">
            
                <a href="01_php介绍.html">
            
                    
                    php介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02_PHP基础语法.html">
            
                <a href="02_PHP基础语法.html">
            
                    
                    php基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03_php变量.html">
            
                <a href="03_php变量.html">
            
                    
                    php变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="04_php数据类型.html">
            
                <a href="04_php数据类型.html">
            
                    
                    php数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05_php常量.html">
            
                <a href="05_php常量.html">
            
                    
                    php常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06_php运算符.html">
            
                <a href="06_php运算符.html">
            
                    
                    php运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="07_php流程控制.html">
            
                <a href="07_php流程控制.html">
            
                    
                    php流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="08_php数组.html">
            
                <a href="08_php数组.html">
            
                    
                    php数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="09_php函数.html">
            
                <a href="09_php函数.html">
            
                    
                    php函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="10_php面向对象.html">
            
                <a href="10_php面向对象.html">
            
                    
                    php面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.11" data-path="11_php命名空间.html">
            
                <a href="11_php命名空间.html">
            
                    
                    php命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="12_php日期和时间.html">
            
                <a href="12_php日期和时间.html">
            
                    
                    php日期时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="13_php表单.html">
            
                <a href="13_php表单.html">
            
                    
                    php表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="14_php包含文件.html">
            
                <a href="14_php包含文件.html">
            
                    
                    php包含文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="15_php常用函数.html">
            
                <a href="15_php常用函数.html">
            
                    
                    php常用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../php高级/16_php常用变量函数.md">
            
                <span>
            
                    
                    php常用变量函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../php高级/01_php错误与异常.html">
            
                <a href="../php高级/01_php错误与异常.html">
            
                    
                    php错误与异常
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../php高级/02_PhpStorm本地断点调试.html">
            
                <a href="../php高级/02_PhpStorm本地断点调试.html">
            
                    
                    PhpStorm本地断点调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../php高级/03_php文件操作.html">
            
                <a href="../php高级/03_php文件操作.html">
            
                    
                    php文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../php高级/04_cookie机制.html">
            
                <a href="../php高级/04_cookie机制.html">
            
                    
                    cookie机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../php高级/05_session.html">
            
                <a href="../php高级/05_session.html">
            
                    
                    session
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../php高级/06_php画图.html">
            
                <a href="../php高级/06_php画图.html">
            
                    
                    php画图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../php高级/07_php加密机制.html">
            
                <a href="../php高级/07_php加密机制.html">
            
                    
                    php加密机制
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php命名空间</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="php&#x547D;&#x540D;&#x7A7A;&#x95F4;">PHP&#x547D;&#x540D;&#x7A7A;&#x95F4;</h1>
<blockquote>
<p>&#x5728;PHP&#x4E2D;&#xFF0C;&#x51FA;&#x73B0;&#x540C;&#x540D;&#x51FD;&#x6570;&#x6216;&#x662F;&#x540C;&#x540D;&#x7C7B;&#x662F;&#x4E0D;&#x88AB;&#x5141;&#x8BB8;&#x7684;&#x3002;&#x4E3A;&#x9632;&#x6B62;&#x7F16;&#x7A0B;&#x4EBA;&#x5458;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x540D;&#x6216;&#x51FD;&#x6570;&#x540D;&#x51FA;&#x73B0;&#x91CD;&#x590D;&#x51B2;&#x7A81;&#xFF0C;&#x5728;PHP5.3&#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x8FD9;&#x4E00;&#x6982;&#x5FF5;&#x3002;</p>
</blockquote>
<h3 id="&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1F;">&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1F;</h3>
<p>&#x5728;&#x505A;&#x9879;&#x76EE;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x53EF;&#x80FD;&#x4F1A;&#x5F15;&#x5165;&#x591A;&#x4E2A;&#x6587;&#x4EF6;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x5F15;&#x5165;&#x7684;&#x591A;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x53EF;&#x80FD;&#x5B58;&#x5728;&#x540C;&#x540D;&#x7684;&#x7C7B;&#x3001;&#x51FD;&#x6570;&#x3001;&#x5E38;&#x91CF;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x9519;&#xFF08;&#x91CD;&#x590D;&#x5B9A;&#x4E49;&#x7684;&#x9519;&#x8BEF;&#xFF09;&#xFF0C;&#x56E0;&#x6B64;PHP&#x5728;5.3&#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x89E3;&#x51B3;&#x8BE5;&#x95EE;&#x9898;&#x3002;</p>
<h3 id="&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x89E3;&#x51B3;&#x95EE;&#x9898;">&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x89E3;&#x51B3;&#x95EE;&#x9898;</h3>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x4EE5;&#x4E0B;&#x4E24;&#x7C7B;&#x95EE;&#x9898;&#xFF1A;</p>
<ol>
<li>&#x7528;&#x6237;&#x7F16;&#x5199;&#x7684;&#x4EE3;&#x7801;&#x4E0E;PHP&#x5185;&#x90E8;&#x7684;&#x7C7B;/&#x51FD;&#x6570;/&#x5E38;&#x91CF;&#x6216;&#x7B2C;&#x4E09;&#x65B9;&#x7C7B;/&#x51FD;&#x6570;/&#x5E38;&#x91CF;&#x4E4B;&#x95F4;&#x7684;&#x540D;&#x5B57;&#x51B2;&#x7A81;&#x3002;</li>
<li>&#x4E3A;&#x5F88;&#x957F;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x540D;&#x79F0;(&#x901A;&#x5E38;&#x662F;&#x4E3A;&#x4E86;&#x7F13;&#x89E3;&#x7B2C;&#x4E00;&#x7C7B;&#x95EE;&#x9898;&#x800C;&#x5B9A;&#x4E49;&#x7684;)&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x522B;&#x540D;&#xFF08;&#x6216;&#x7B80;&#x77ED;&#xFF09;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x63D0;&#x9AD8;&#x6E90;&#x4EE3;&#x7801;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#x3002;</li>
</ol>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x5373;&#x5C06;&#x4EE3;&#x7801;&#x5212;&#x5206;&#x6210;&#x4E0D;&#x540C;&#x7A7A;&#x95F4;&#xFF0C;&#x4E0D;&#x540C;&#x7A7A;&#x95F4;&#x7684;&#x7C7B;&#x540D;&#x76F8;&#x4E92;&#x72EC;&#x7ACB;&#xFF0C;&#x4E92;&#x4E0D;&#x51B2;&#x7A81;&#x3002;</p>
<blockquote>
<p>&#x7406;&#x89E3;&#xFF1A;
&#x6CA1;&#x6709;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1A; &#x4E00;&#x4E2A;&#x623F;&#x95F4;&#x6709;2&#x4E2A;&#x5F20;&#x4E09;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x53EB;&#x5F20;&#x4E09;&#xFF0C;&#x4E24;&#x4E2A;&#x4EBA;&#x90FD;&#x4F1A;&#x56DE;&#x7B54;&#x3002;
&#x6709;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1A;A&#x623F;&#x95F4;&#x653E;&#x4E00;&#x4E2A;&#x5F20;&#x4E09;&#xFF0C;B&#x623F;&#x95F4;&#x653E;&#x4E00;&#x4E2A;&#x5F20;&#x4E09; &#xFF0C;&#x6211;&#x627E;A&#x623F;&#x95F4;&#x5F20;&#x4E09;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x6709;A&#x5F20;&#x4E09;&#x56DE;&#x7B54;&#x3002;B&#x5F20;&#x4E09;&#x5C31;&#x77E5;&#x9053;&#x4E0D;&#x662F;&#x4ED6;&#x3002;</p>
</blockquote>
<h2 id="&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5206;&#x7C7B;">&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5206;&#x7C7B;</h2>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5206;&#x4E3A;&#x4E24;&#x7C7B;&#xFF1A;<strong>&#x5168;&#x5C40;&#x7A7A;&#x95F4;</strong> &#x548C; <strong>&#x547D;&#x540D;&#x7A7A;&#x95F4;</strong></p>
<h3 id="&#x5168;&#x5C40;&#x7A7A;&#x95F4;">&#x5168;&#x5C40;&#x7A7A;&#x95F4;</h3>
<p>&#x5168;&#x5C40;&#x7A7A;&#x95F4;&#xFF1A;&#x4E0D;&#x5728;namespace&#x58F0;&#x660E;&#x7684;&#x7A7A;&#x95F4;&#x91CC;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x90FD;&#x5C5E;&#x4E8E;&#x5168;&#x5C40;&#x7A7A;&#x95F4;&#x3002;</p>
<pre><code>// new&#x7C7B;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x7C7B;&#x524D;&#x52A0;&#x4E0A;&#x53CD;&#x659C;&#x6760;\,&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x52A0;&#x3002;
//1.php   
class Person{
    function __construct(){
            echo &apos;I am Person!&apos;;
        }
}
//name.php
require_once &apos;./1.php&apos;;

new Person();     //&#x8F93;&#x51FA; I am Person!;
new \Person();    //&#x8F93;&#x51FA; I am Person!;
</code></pre><h3 id="&#x547D;&#x540D;&#x7A7A;&#x95F4;">&#x547D;&#x540D;&#x7A7A;&#x95F4;</h3>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1A;&#x5982;&#x679C;&#x9700;&#x8981;&#x5728;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x7A7A;&#x95F4;&#x7684;&#x7C7B;&#x3001;&#x51FD;&#x6570;&#x3001;&#x5E38;&#x91CF;&#xFF0C;&#x5728;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x52A0;&#x4E0A;&#x53CD;&#x659C;&#x7EBF;&#xFF08;&#x4EE3;&#x8868;&#x5168;&#x5C40;&#x7A7A;&#x95F4;&#xFF09;
&#x5982;&#xFF1A;<code>namespace Think</code>;
&#x5982;&#xFF1A;<code>namespace Think\Controller</code>; &#x8FD9;&#x79CD;&#x53CD;&#x659C;&#x7EBF;&#x5206;&#x5272;&#x7684;&#x3001;&#x5C42;&#x6B21;&#x5316;&#x7684;&#xFF0C;&#x5C31;&#x662F;&#x5B50;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x3002;</p>
<h2 id="&#x5B9A;&#x4E49;&#x547D;&#x540D;&#x7A7A;&#x95F4;">&#x5B9A;&#x4E49;&#x547D;&#x540D;&#x7A7A;&#x95F4;</h2>
<blockquote>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6240;&#x6709;&#x5E38;&#x91CF;&#x3001;&#x7C7B;&#x548C;&#x51FD;&#x6570;&#x540D;&#x90FD;&#x653E;&#x5728;&#x5168;&#x5C40;&#x7A7A;&#x95F4;&#x4E0B;&#xFF0C;&#x5C31;&#x548C;PHP&#x652F;&#x6301;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E4B;&#x524D;&#x4E00;&#x6837;&#x3002;</p>
</blockquote>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x901A;&#x8FC7;&#x5173;&#x952E;&#x5B57;namespace &#x6765;&#x58F0;&#x660E;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x5305;&#x542B;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x5B83;&#x5FC5;&#x987B;&#x5728;&#x5176;&#x5B83;&#x6240;&#x6709;&#x4EE3;&#x7801;&#x4E4B;&#x524D;&#x58F0;&#x660E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x3002;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1B;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>  
<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4EE3;&#x7801;&#x5728; &apos;MyProject&apos; &#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;  </span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">MyProject</span>;  

<span class="hljs-comment">// ... &#x4EE3;&#x7801; ...</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x524D;&#x4E0D;&#x80FD;&#x6709;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x3002;(&#x5982;&#x6709;&#xFF0C;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;fatal error)&#x5982;html&#x4EE3;&#x7801;&#x7B49;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x58F0;&#x660E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E4B;&#x524D;&#x552F;&#x4E00;&#x5408;&#x6CD5;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x7528;&#x4E8E;&#x5B9A;&#x4E49;&#x6E90;&#x6587;&#x4EF6;&#x7F16;&#x7801;&#x65B9;&#x5F0F;&#x7684; declare &#x8BED;&#x53E5;&#xFF0C;&#x5982;&#xFF1A;declare(encoding=&apos;UTF-8&apos;);</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E0B;&#xFF0C;&#x867D;&#x7136;&#x53EF;&#x4EE5;&#x653E;&#x7F6E;&#x6240;&#x6709;&#x5408;&#x6CD5;&#x7684;php&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x53D7;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5F71;&#x54CD;&#x7684;&#x4EC5;&#x6709;&#x7C7B;&#x3001;&#x5E38;&#x91CF;&#x548C;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x901A;&#x5E38;&#x8BE5;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x8981;&#x9075;&#x5FAA;PSR-0&#x547D;&#x540D;&#x89C4;&#x8303;&#xFF08;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x548C;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#xFF09;&#x3002;</p>
</blockquote>
<pre><code># &#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x4F1A;&#x51FA;&#x73B0;&#x8BED;&#x6CD5;&#x9519;&#x8BEF;&#xFF1A;
&lt;html&gt;
&lt;?php
namespace MyProject; // &#x547D;&#x540D;&#x7A7A;&#x95F4;&#x524D;&#x51FA;&#x73B0;&#x4E86;&#x201C;&lt;html&gt;&#x201D; &#x4F1A;&#x81F4;&#x547D;&#x9519;&#x8BEF; -&#x3000;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5FC5;&#x987B;&#x662F;&#x7A0B;&#x5E8F;&#x811A;&#x672C;&#x7684;&#x7B2C;&#x4E00;&#x6761;&#x8BED;&#x53E5;
?&gt;
</code></pre><h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h3>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?PHP</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">teacher</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;Teacher Person&quot;</span>;
    }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Teach</span><span class="hljs-params">()</span></span>{
    <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;Teacher Teach&quot;</span>;
}

<span class="hljs-keyword">const</span> hello = <span class="hljs-string">&quot;hello Teacher&quot;</span>;


<span class="hljs-keyword">namespace</span> <span class="hljs-title">student</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;Student Person&quot;</span>;
    }
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Teach</span><span class="hljs-params">()</span></span>{
    <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;Student Teach&quot;</span>;
}

<span class="hljs-keyword">const</span> hello = <span class="hljs-string">&quot;hello Student&quot;</span>;

<span class="hljs-keyword">new</span> Person();          <span class="hljs-comment">//echo &quot;Student Person&quot;   &#x8C03;&#x7528;&#x672C;&#x7A7A;&#x95F4;&#x4E0B;&#x7684; Person&#x7C7B;</span>
<span class="hljs-keyword">new</span> \teacher\Person(); <span class="hljs-comment">//echo &quot;Teacher Person&quot;   &#x8C03;&#x7528;teacher&#x4E0B;&#x7684;Person&#x7C7B;</span>
<span class="hljs-keyword">new</span> \student\Person(); <span class="hljs-comment">//echo &quot;Student Person&quot;   &#x8C03;&#x7528;student&#x4E0B;&#x7684;Person&#x7C7B;</span>
\teacher\Teach();      <span class="hljs-comment">//echo &quot;Teacher Teach&quot;    &#x8C03;&#x7528;teacher&#x4E0B;&#x7684;Teach&#x51FD;&#x6570;</span>
<span class="hljs-keyword">echo</span> \teacher\hello;   <span class="hljs-comment">//echo &quot;hello Teacher&quot;    &#x8BBF;&#x95EE;teacher&#x4E0B;&#x7684;hello&#x5E38;&#x91CF;</span>
<span class="hljs-keyword">echo</span> hello;            <span class="hljs-comment">//echo &quot;hello Student&quot;    &#x8BBF;&#x95EE;student&#x4E0B;&#x7684;hello&#x5E38;&#x91CF;</span>
<span class="hljs-keyword">echo</span> \student\hello;   <span class="hljs-comment">//echo &quot;hello Student&quot;    &#x8BBF;&#x95EE;student&#x4E0B;&#x7684;hello&#x5E38;&#x91CF;</span>
</code></pre>
<h3 id="&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x591A;&#x4E2A;&#x7A7A;&#x95F4;">&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x591A;&#x4E2A;&#x7A7A;&#x95F4;</h3>
<p>&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x5B9A;&#x4E49;&#x7684;&#x8BED;&#x6CD5;&#x6709;&#x4E24;&#x79CD;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x7B80;&#x5355;&#x7EC4;&#x5408;&#x8BED;&#x6CD5;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x662F;&#x5927;&#x62EC;&#x53F7;&#x5F62;&#x5F0F;&#x8BED;&#x6CD5;&#xFF0C;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x4F7F;&#x7528;&#x4E00;&#x822C;&#x662F;&#x591A;&#x4E2A;&#x6587;&#x4EF6;&#x5408;&#x5E76;&#x6210;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x4F46;&#x4E0D;&#x5230;&#x4E07;&#x4E0D;&#x5F97;&#x5DF2;&#x6700;&#x597D;&#x4E0D;&#x8981;&#x8FD9;&#x6837;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x589E;&#x52A0;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x590D;&#x6742;&#x5EA6;&#xFF0C;&#x53EF;&#x8BFB;&#x6027;&#x4F1A;&#x964D;&#x4F4E;&#xFF0C;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E5F;&#x6CA1;&#x6709;&#x8FD9;&#x79CD;&#x4F7F;&#x7528;&#x7684;&#x5FC5;&#x8981;&#x3002;</p>
<h4 id="&#x7B80;&#x5355;&#x7EC4;&#x5408;&#x8BED;&#x6CD5;">&#x7B80;&#x5355;&#x7EC4;&#x5408;&#x8BED;&#x6CD5;</h4>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Index</span>;
<span class="hljs-keyword">const</span> NUM=<span class="hljs-number">1</span>;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">Col</span>;
<span class="hljs-keyword">const</span> NUM=<span class="hljs-number">2</span>;
</code></pre>
<h4 id="&#x5927;&#x62EC;&#x53F7;&#x5F62;&#x5F0F;&#x8BED;&#x6CD5;">&#x5927;&#x62EC;&#x53F7;&#x5F62;&#x5F0F;&#x8BED;&#x6CD5;</h4>
<p>&#x5927;&#x62EC;&#x53F7;&#x8BED;&#x6CD5;&#xFF0C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x591A;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x8FD8;&#x9700;&#x8981;&#x5199;&#x4E0A;&#x975E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5C31;&#x53EA;&#x80FD;&#x7528;&#x5927;&#x62EC;&#x53F7;&#x8BED;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x975E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4EE3;&#x7801;&#x7528;namespace&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x540D;&#x79F0;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x518D;&#x7528;&#x5927;&#x62EC;&#x53F7;&#x5373;&#x53EF;</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">/*&#x547D;&#x540D;&#x7A7A;&#x95F4;Index*/</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Index</span>{
    <span class="hljs-title">const</span> <span class="hljs-title">NUM</span>=1;
}

<span class="hljs-comment">/*&#x547D;&#x540D;&#x7A7A;&#x95F4;Col*/</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Col</span>{
    <span class="hljs-title">const</span> <span class="hljs-title">NUM</span>=2;
}

<span class="hljs-comment">/*&#x5168;&#x5C40;&#x975E;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4EE3;&#x7801;*/</span>
<span class="hljs-keyword">namespace</span> {
    <span class="hljs-title">const</span> <span class="hljs-title">NUM</span>=3;
}
</code></pre>
<p>&#x591A;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x540C;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x540C;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x5185;&#x5BB9;&#x53EF;&#x4EE5;&#x5206;&#x522B;&#x5B58;&#x50A8;&#x5230;&#x591A;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x6587;&#x4EF6;&#x4E2D;&#x3002;</p>
<h3 id="&#x5B50;&#x547D;&#x540D;&#x7A7A;&#x95F4;">&#x5B50;&#x547D;&#x540D;&#x7A7A;&#x95F4;</h3>
<p>&#x4E0E;&#x76EE;&#x5F55;&#x548C;&#x6587;&#x4EF6;&#x7684;&#x5173;&#x7CFB;&#x5F88;&#x8C61;&#xFF0C;PHP &#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E5F;&#x5141;&#x8BB8;&#x6307;&#x5B9A;&#x5C42;&#x6B21;&#x5316;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x540D;&#x79F0;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x540D;&#x5B57;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5206;&#x5C42;&#x6B21;&#x7684;&#x65B9;&#x5F0F;&#x5B9A;&#x4E49;&#xFF0C;&#x5206;&#x9694;&#x7B26;&#x662F;<code>\</code></p>
<pre><code class="lang-php">&lt;?php
namespace school\teacher;
class Person(){
    function __construct(){
        echo &quot;hello teacher&quot;;
    }
}

namespace school\student;
class Person(){
    function __construct(){
        echo &quot;hello teacher&quot;;
    }
}

new \school\teacher\Person();   //echo &quot;hello teacher&quot;
new \school\student\Person();
</code></pre>
<h3 id="&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x8BC6;&#x522B;&#x539F;&#x7406;">&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x8BC6;&#x522B;&#x539F;&#x7406;</h3>
<p>&#x6CA1;&#x6709;&#x9650;&#x5B9A;&#x540D;&#x79F0;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x8981;&#x8BFB;&#x53D6;&#x7684;&#x7C7B;&#x3001;&#x5E38;&#x91CF;&#x3001;&#x51FD;&#x6570;&#x3001;&#x63A5;&#x53E3;&#x540D;&#x79F0;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4F1A;&#x8BFB;&#x53D6;&#x8BE5;&#x5185;&#x5BB9;&#x6240;&#x5C5E;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x7C7B;&#x3001;&#x5E38;&#x91CF;&#x3001;&#x51FD;&#x6570;&#x3001;&#x63A5;&#x53E3;&#x540D;&#x79F0;&#xFF0C;&#x4F46;&#x5982;&#x679C;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5185;&#x6CA1;&#x6709;&#x76F8;&#x5173;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x7C7B;&#x548C;&#x63A5;&#x53E3;&#x540D;&#x79F0;&#x4F1A;&#x8FD4;&#x56DE;fatal error&#xFF0C;&#x5982;&#x679C;&#x662F;&#x51FD;&#x6570;&#x548C;&#x5E38;&#x91CF;&#x4F1A;&#x81EA;&#x52A8;&#x8BFB;&#x53D6;&#x5168;&#x5C40;&#x7684;&#x51FD;&#x6570;&#x548C;&#x5E38;&#x91CF;&#xFF0C;&#x5982;&#x679C;&#x5168;&#x5C40;&#x4E2D;&#x4E5F;&#x6CA1;&#x6709;&#xFF0C;&#x624D;&#x4F1A;&#x62A5;fatal error&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1A;&#x522B;&#x540D;&#x5BFC;&#x5165;">&#x4F7F;&#x7528;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF1A;&#x522B;&#x540D;/&#x5BFC;&#x5165;</h2>
<p>&#x8BED;&#x6CD5;&#xFF1A;use &#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;&#x53EF;&#x4EE5;&#x8D77;&#x522B;&#x540D;&#xFF08;as &#x522B;&#x540D;&#xFF09;&#x3002;</p>
<p>&#x76EE;&#x7684;&#xFF1A;&#x5728;&#x5F53;&#x524D;&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684; &#x7C7B;&#x3001;&#x51FD;&#x6570;&#x3001;&#x5E38;&#x91CF;&#x3002;&#xFF08;&#x4F7F;&#x7528;&#x65F6;&#xFF0C;&#x5C31;&#x4E0D;&#x7528;&#x52A0;&#x4E0A;&#x9650;&#x5B9A;&#x540D;&#x79F0;&#xFF09;</p>
<pre><code class="lang-php"><span class="hljs-comment"># index.php</span>
<span class="hljs-meta">&lt;?php</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">index</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Teacher</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;index Teacher&quot;</span>;
    }
}
<span class="hljs-meta">?&gt;</span>

<span class="hljs-comment">#test.php</span>
<span class="hljs-meta">&lt;?php</span>
<span class="hljs-keyword">namespace</span>  <span class="hljs-title">test</span>;
<span class="hljs-keyword">include</span> <span class="hljs-string">&quot;index.php&quot;</span>;

<span class="hljs-comment">//&#x7ED9; index&#x7A7A;&#x95F4;&#x4E0B; Teacher &#x7C7B;&#x8D77;&#x4E2A;&#x522B;&#x540D; teacher</span>
<span class="hljs-keyword">use</span> \<span class="hljs-title">index</span>\<span class="hljs-title">Teacher</span> <span class="hljs-title">as</span> <span class="hljs-title">teacher</span>;
<span class="hljs-keyword">new</span> teacher();    <span class="hljs-comment">//echo &quot;index Teacher&quot;</span>
<span class="hljs-meta">?&gt;</span>
</code></pre>
<h2 id="&#x53C2;&#x8003;">&#x53C2;&#x8003;</h2>
<ul>
<li>&#x83DC;&#x9E1F;&#x6559;&#x7A0B; PHP &#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="http://www.runoob.com/php/php-namespace.html" target="_blank">http://www.runoob.com/php/php-namespace.html</a></li>
<li><a href="https://blog.csdn.net/belen_xue/article/details/78875377" target="_blank">https://blog.csdn.net/belen_xue/article/details/78875377</a></li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="10_php面向对象.html" class="navigation navigation-prev " aria-label="Previous page: php面向对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="12_php日期和时间.html" class="navigation navigation-next " aria-label="Next page: php日期时间">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php命名空间","level":"1.2.11","depth":2,"next":{"title":"php日期时间","level":"1.2.12","depth":2,"path":"php/php基础/12_php日期和时间.md","ref":"./php/php基础/12_php日期和时间.md","articles":[]},"previous":{"title":"php面向对象","level":"1.2.10","depth":2,"path":"php/php基础/10_php面向对象.md","ref":"./php/php基础/10_php面向对象.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"php/php基础/11_php命名空间.md","mtime":"2020-04-23T09:59:52.391Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T11:06:38.883Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

